#include "page.h"
#include "document.h"
#include "pagecontrol.h"
#include "backgrounds/blankBG.h"

#include <KMessageBox>
#include <QDebug>

Page::Page(Document *parent): QObject(parent), QGraphicsItem()
{
    init(parent, new BlankBackground(this));
}

Page::Page(Document* parent, BGSource* background) : QObject(parent), QGraphicsItem()
{
    init(parent, background);
}

void Page::init(Document* parent, BGSource* background)
{
    m_bgPixmap = 0;
    m_parentDocument = parent;
    m_parentDocument->scene()->addItem(this);

    m_bgsource = background;
    connect(m_bgsource, SIGNAL(bgRendered(QPixmap*)), this, SLOT(render(QPixmap*)));

    // Move the page into the background.
    this->setZValue(-1);

    // TODO: What's with the page size?
    m_realSize = QSizeF(700,1000);

    // Configure the PageControl element.
    m_pageControl = new PageControl(this);
    repositionChildren();
}

void Page::repositionChildren()
{
    qreal centerX = (boundingRect().width() / 2) - m_pageControl->boundingRect().width() / 2;
    m_pageControl->setPos(centerX, sceneSize().height());
}

QRectF Page::drawingRect() const
{
    return QRectF(QPointF(0,0), sceneSize());
}

QRectF Page::boundingRect() const
{
    return QRectF(QPointF(0,0), sceneSize() + QSizeF(0, m_pageControl->boundingRect().height()));
}

/**
 * Not giving parameters a name shows, that they are not used and the compiler
 * won't give warning.
 */
void Page::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
{
    // Nothing to paint, as only the children are visible and they are painted by default.
    if (!m_bgPixmap && !m_bgsource->rendering())
    {
        m_bgsource->renderBackgroundPixmap(sceneSize());
    }
}

QSizeF Page::sceneSize() const
{
    // Multiply the actual size with the current scale.
    return m_realSize * m_parentDocument->scale();
}

void Page::render(QPixmap* pixmap)
{
    if (m_bgPixmap)
    {
        delete m_bgPixmap;
    }
    m_bgPixmap = new QGraphicsPixmapItem(*pixmap, this);
    m_bgPixmap->setZValue(-1);
}

void Page::addPageAfter()
{
    // TODO: Implement addPageAfter.
    m_parentDocument->addNewPage(this);
}

void Page::duplicate()
{
    m_parentDocument->insertPage(this, this->deepCopy());
}

void Page::remove()
{
    m_parentDocument->removePage(this);
}

void Page::moveDown()
{
    m_parentDocument->movePageDown(this);
}

void Page::moveUp()
{
   m_parentDocument->movePageUp(this);
}

void Page::setScale(qreal oldScale, qreal newScale, qreal scaleFactor)
{
    QListIterator<QGraphicsItem*> iter(childItems());
    QGraphicsItem *current;

    m_bgsource->renderBackgroundPixmap(sceneSize());

    while (iter.hasNext()) {
        current = iter.next();
        if (current->type() == PageControl::Type ||
            (current->type() == QGraphicsPixmapItem::Type && current->zValue() == -1)) {
            continue;

        } else {
            /* The scale is not set to the newScale, to keep a consistent scaling behaviour for
             * elements that have been created with a scale different than 1. These have to be
             * scaled relatively to the new scale. */
            current->setScale(current->scale() *  scaleFactor);
        }
    }
}

Page* Page::deepCopy()
{
    Page *result = new Page(m_parentDocument, m_bgsource->deepCopy());
    result->m_realSize = m_realSize;

    // TODO: Copy QGraphicsItems.

    return result;
}

